<?php
class MessageTrack extends AppModel {
	var $name = 'MessageTrack';
	var $validate = array(
		'message_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
			),
		),
		'to_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
			),
		),
		'from_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
			),
		)
	);
	//The Associations below have been created with all possible keys, those that are not needed can be removed

	var $belongsTo = array(
		'Message' => array(
			'className' => 'Message',
			'foreignKey' => 'message_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
            'Sender' => array(
            'className' => 'User',
            'foreignKey' => 'from_id'
        ),
        'Recipient' => array(
            'className' => 'User',
            'foreignKey' => 'to_id'
        )

	);

        function lastMsg($buyerId, $sellerId){
            $this->Behaviors->attach('Containable');
            $this->contain('Message');
            $msgs=$this->find('all', array('conditions'=>array('MessageTrack.to_id'=>$buyerId, 'MessageTrack.from_id'=>$sellerId, 'MessageTrack.to_group'=>'Subscribed')));
            
            if(count($msgs)==0){
                return 0;
            }

            foreach ($msgs as $msg){
                $time[]=strtotime($msg['Message']['created']);
            }
            return max($time);
        }
}
?>